package com.meiyou.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.meiyou.pojo.Coordinate;
import com.meiyou.pojo.MeiyouUser;
import com.meiyou.service.RedisService;
import com.meiyou.test.RedisUtil;
import com.meiyou.utils.Msg;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import redis.clients.jedis.GeoRadiusResponse;

@Controller
@Api(value="Redis有关控制器",tags = { "Redis" })
public class RedisController {
    
	@Autowired
	RedisService redis;
	@RequestMapping(value="nearpesons",method=RequestMethod.POST)
	@ApiOperation("获取附近的人信息")
	public @ResponseBody Msg nearpesons(double jindu,double weidu,HttpServletRequest req) {
		Msg msg;
		if(req.getSession().getAttribute("user")==null) {
			System.out.println("没有登录");
			msg = Msg.fail();
			msg.setCode(300);
			msg.setMsg("请先登录");
			return msg;
		}
		String uccount = req.getSession().getAttribute("user").toString();
		System.out.println("获取附近的人"+jindu+","+weidu);
		Coordinate coordinate = new Coordinate();
		coordinate.setLatitude(weidu);
		coordinate.setLongitude(jindu);
		coordinate.setKey(uccount);
		
		//Long result = RedisUtil.addReo(coordinate, "test");
		/*
		 * if(result==null) System.out.println("上传位置信息失败");
		 */
		List<GeoRadiusResponse> list = RedisUtil.addReoAndQuery(coordinate, 100);
		Map<String,Object> map1 = new HashMap<String,Object>();
		List<Object> ls = new ArrayList<Object>();
		if(list!=null) {
			for(GeoRadiusResponse geo:list) {
				MeiyouUser user = redis.selUserInfoByphont(geo.getMemberByString());
				Map<String,Object> map = new HashMap<String,Object>();
				if(geo.getDistance()<0.1)
					geo.setDistance(0.1);
				if(req.getSession().getAttribute("user")==null) {
					user.setOnline(false);
				}else {
					user.setOnline(true);
				}			
				map.put("uname", user.getuName()); //用户昵称
				map.put("distance", geo.getDistance()); //距离
				map.put("online", user.getOnline());//是否在线
				map.put("ucount",user.getuCount());//用户账号
				map.put("img",user.getuImg()); //用户头像
				map.put("sex",user.getuSex()); //用户性别
				map.put("age", user.getuOld());//用户年龄
				map.put("qianming",user.getQianming());
				ls.add(map);	
			}
		}
		System.out.println("获取附近人结果大小:"+ls.size());
		map1.put("persons", ls);
		msg = Msg.success();
		msg.setExtend(map1);
		return msg;
	}
	
	@RequestMapping(value="nearpesonsBysex",method=RequestMethod.POST)
	@ApiOperation("获取附近的人信息")
	public @ResponseBody Msg nearpesonsBysex(double jindu,double weidu,String uccount,String sex,HttpServletRequest req) {
		Msg msg;
		Coordinate coordinate = new Coordinate();
		coordinate.setLatitude(weidu);
		coordinate.setLongitude(jindu);
		coordinate.setKey(uccount);
		RedisUtil.addReo(coordinate, "test");
		List<GeoRadiusResponse> list = RedisUtil.geoQuery(coordinate, 100);
		Map<String,Object> map1 = new HashMap<String,Object>();
		List<Object> ls = new ArrayList<Object>();
		if(list!=null) {
			for(GeoRadiusResponse geo:list) {
				MeiyouUser user = redis.selUserInfoByphont(geo.getMemberByString());
				Map<String,Object> map = new HashMap<String,Object>();
				if(geo.getDistance()<0.1)
					geo.setDistance(0.1);
				if(req.getSession().getAttribute("user")==null) {
					user.setOnline(false);
				}else {
					user.setOnline(true);
				}
				if(user.getuSex().equals(sex)) {
					map.put("uname", user.getuName()); //用户昵称
					map.put("distance", geo.getDistance()); //距离
					map.put("online", user.getOnline());//是否在线
					map.put("ucount",user.getuCount());//用户账号
					map.put("img",user.getuImg()); //用户头像
					map.put("sex",user.getuSex()); //用户性别
					map.put("qianming",user.getQianming());
				}
				if(map.size()!=0) {
					ls.add(map);	
				}
			}
		}
		
		map1.put("persons", list.size());
		msg = Msg.success();
		msg.setExtend(map1);
		return msg;
	}
	
	
}
